package com.javaee.xiangyang.controller.Android;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.OrderGoods;
import com.javaee.xiangyang.entity.Order;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.mapper.GoodsMapper;
import com.javaee.xiangyang.service.AccountService;
import com.javaee.xiangyang.service.OrderService;
import com.javaee.xiangyang.vo.ServerResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Order")
public class OrderController {

    @Resource
    OrderService orderService;

    @Resource
    AccountService accountService;

    /**
     * 用户传递订单信息
     * 有用户ID 商品信息
     */
    @PostMapping("/InsertOrder")
    public ServerResponse<Order> insertOrder(Order order) {
        //重置时间
        order.setOrderStart(new Date());
        ServiceResultEnum serviceResultEnum = orderService.insertOrder(order);
        if (serviceResultEnum == ServiceResultEnum.SUCCESS) {
            //如果成功
            return ServerResponse.creatServerResponseBySuccess();
        }
        //失败
        return ServerResponse.creatServerResponseByFail(serviceResultEnum.getResult());
    }

    /**
     * 用户根据用户Id查询用户所拥有的订单
     */
    @GetMapping("/FindOrderByUserAccount")
    public ServerResponse<List<OrderGoods>> findOrderByUserAccount(Order order) {
        User user = accountService.GetUser(new User(order.getOrderUserAccount()));
        if (user != null) {
            //用户存在
            List<OrderGoods> orderByUserAccount = orderService.findOrderByUserAccount(order);
            return ServerResponse.creatServerResponseBySuccess(orderByUserAccount);
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
    }

    /**
     * 根据订单编号查询具体订单信息
     */
    @GetMapping("/FindOrderByOrderId")
    public ServerResponse<OrderGoods> findOrderByOrderId(Order order) {
        OrderGoods orderByOrderId = orderService.findOrderByOrderId(order);
        if (orderByOrderId != null) {
            return ServerResponse.creatServerResponseBySuccess(orderByOrderId);
        }
        return ServerResponse.creatServerResponseByFail(ServiceResultEnum.ORDER_NOT_EXIST_ERROR.getResult());
    }

    /**
     * 根据订单编号更改订单状态
     */
    @PostMapping("/UpdateStateByOrderId")
    public ServerResponse<OrderGoods> updateOrderStateByOrderId(Order order) {
        ServiceResultEnum serviceResultEnum = orderService.updateOrderStateByOrderId(order);
        if (serviceResultEnum == ServiceResultEnum.SUCCESS)
            return ServerResponse.creatServerResponseBySuccess();
        return ServerResponse.creatServerResponseByFail(serviceResultEnum.getResult());
    }

}
